package com.dalongxin.work.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
import android.widget.TextView;

import com.dalongxin.work.R;

/**
 * Created by carlos on 2017/5/26.
 */

public class LoadingProgressDialog extends Dialog {
    private ImageView vLoadingImg;//指示器对应图片
    private AnimationDrawable mAnimation;// 进度图片动画
    private TextView vMessageTextView; // 加载提示文本

    public LoadingProgressDialog(Context context) {
        super(context);
    }

    public LoadingProgressDialog(Context context, int theme) {
        super(context, theme);
        setContentView(R.layout.loading_progress_dialog);
        initView();
        initImg();
        initListener();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        vLoadingImg = (ImageView) findViewById(R.id.dialog_loading_dialog_img);
        vMessageTextView = (TextView) findViewById(R.id.loading_progress_message);
    }

    /**
     * 初始化图片动画
     */
    private void initImg() {
        mAnimation = (AnimationDrawable) vLoadingImg.getBackground();
    }

    /**
     * 初始化监听
     */
    private void initListener() {
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                stop();
                clearRes();
            }
        });
    }

    /**
     * 初始化数据
     */
    public void setMessage(String message) {
        vMessageTextView.setText(message);
    }

    /**
     * 开始动画
     */
    private void start() {
        if (mAnimation != null) {
            mAnimation.start();
        }
    }

    /**
     * 停止动画
     */
    private void stop() {
        if (mAnimation != null) {
            mAnimation.stop();
        }
    }

    /**
     * 回收对象
     */
    private void clearRes() {
        mAnimation = null;
        vLoadingImg = null;
    }

    @Override
    public void show() {
        super.show();

        start();
    }
}
